package com.dayuanit.dy15.lesson21.demo2;

import java.lang.reflect.Array;
import java.util.*;

public class Test {

    //用JDK自带的排序
    public static void sort(Word[] words) {
        Arrays.sort(words);
    }

    //冒泡排序
    public static void mySort(Word[] words) {
        for (int i=0; i<words.length - 1; i++) {
            for (int j=0; j<words.length - i - 1; j++) {
                //从大到小
                if (words[j].count < words[j + 1].count) {
                    Word temp = words[j];
                    words[j] = words[j + 1];
                    words[j + 1] = temp;
                }
            }
        }
    }

    public static void main(String[] args) {

//        作业：
        String msg = "helping young people in need, encouraging the development of physical and social skills through sports and assisting the homeless";
//
//        统计msg的各个英文字母(不是单词)出现的次数。
//        TIPS：可以使用Map的key-value数据结构来做。

        //替换字符串 "," ""  " " ""

        msg = msg.replace(",", "");
        System.out.println(msg);

        msg = msg.replace(" ", "");
        System.out.println(msg);

        //key-字符  value-字符出现的次数
        Map<Character, Integer> map = new HashMap<>();

        char[] chars = msg.toCharArray();
        for (char word : chars) {
            //当前字符是否存在map集合中？
            if (map.containsKey(word)) {
                Integer count = map.get(word);
                count += 1;
                map.put(word, count);

//                Integer count = map.get(word);
//                map.put(word, ++count);

//                Integer count = map.get(word);
//                count ++;
//                map.put(word, count);

//                Integer count = map.get(word);
//                map.put(word, count++);//error
            } else {
                map.put(word, 1);
            }
        }

        System.out.println(map);

        Word[] words = new Word[map.size()];
        int index = 0;
        for (Map.Entry<Character, Integer> entry : map.entrySet()) {
            Word word = new Word(entry.getKey(), entry.getValue());
            words[index] = word;
            index ++;
        }

        System.out.println("---------排序前的------------------");

        for (Word word : words) {
            System.out.println(word);
        }

//        mySort(words);
        sort(words);


        System.out.println("---------排序后的------------------");
        for (Word word : words) {
            System.out.println(word);
        }

        System.out.println("---------------------------");

//        Map<String, Integer> map = new HashMap<>();
//        map.put("a", 3);
//        map.put("b", 5);
//        System.out.println(map.get("a"));
//        System.out.println(map.get("b"));
//        System.out.println(map.get("c"));
//        map.put("a", 6);
//
//        System.out.println(map.get("a"));
//        System.out.println("size=" + map.size());
//
//        Integer numb = map.get("a");
//        System.out.println("numb=" + numb);
//        numb = numb + 1;//先拆箱 再装箱
////        numb = Integer.valueOf(numb.intValue() + 1);
//        System.out.println("numb=" + numb);//7
//        System.out.println(map.get("a"));//6

    }
}
